package com.aote.expression.paramprocessor;

import java.util.Map;

import com.aote.expression.Param;
import com.browsesoft.Tools;
import com.browsesoft.htmlcomponent.HTMLBasicComponent;
import com.browsesoft.user.User;

public class ChineseMoneyProcessor implements ParamProcessor {

	public String process(Param param, Map attributes, User loginUser,
			HTMLBasicComponent component) {
		String result = "零";
		Map map = param.getParams();
		String field = (String) map.get("field");
		String sub = (String) map.get("sub");
		if (sub != null && !sub.equals("")) {
			result = this.CutConvent(attributes, field, sub);
		} else {
			if (attributes.containsKey(field)) {
				String money = attributes.get(field).toString();
				result = Tools.conventLowerToUpper(money);
			}
		}
		return result;
	}

	// 截取转换
	private String CutConvent(Map attributes, String field, String subStr) {
		int sub = Integer.parseInt(subStr);
		if (attributes.containsKey(field)) {
			field = attributes.get(field).toString();
			if (field.length() > sub) {
				if (field.indexOf(".") != -1) {
					String[] str = field.split("\\.");
					if (sub >= 0 && str[0].length() > sub) {
						field = str[0].substring(str[0].length() - sub - 1,
								str[0].length() - sub);
					} else {
						if (sub == -1 && str[1].length() >= 1) {
							field = str[1].substring(0, 1);
						} else if (sub == -2 && str[1].length() == 2) {
							field = str[1].substring(1, 2);
						} else {
							field = "";
						}
					}
				} else {
					field = field.substring(field.length() - sub - 1, field
							.length()
							- sub);
				}
			} else {
				field = "";
			}
		} else {
			field = "";
		}
		if (field.equals("0"))
			return "零";
		if (field.equals(""))
			return "";
		String result = Tools.conventLowerToUpper(field);
		result = result.substring(0, result.length() - 2);
		return result;
	}

}
